package org.veloxbot.api.wrappers;

import org.veloxbot.api.internals.concurrent.Task;

/**
 * @author Velox
 */
public abstract class Condition {

	private final Condition self = this;

	/**
	 * Checks if the condition is met.
	 *
	 * @return Whether the condition is valid or not.
	 */
	public abstract boolean validate();

	/**
	 * Sleeps until the condition is met, or the time is met.
	 *
	 * @param millis The maximum amount of time to sleep for.
	 * @return <t>true</t> if the condition was met before the time was reached, otherwise <t>false</t>.
	 */
	public boolean sleep(final int millis) {
		final long start = System.currentTimeMillis();
		while (System.currentTimeMillis() - start <= millis) {
			if (validate()) {
				return true;
			}
			Task.sleep(5, 10);
		}
		return false;
	}

	/**
	 * Creates an opposite Condition of the current one.
	 *
	 * @return An opposite Condition of the current one.
	 */
	public Condition inverse() {
		return new Condition() {
			public boolean validate() {
				return !self.validate();
			}
		};
	}
}